--
local trace = trace
local import = import
---@type behavior_ret
local bret = import('bt/bt_plugin/behavior3/behavior_ret')
---@type api
local api = import("api/api")
---@type common
local common = import("game/func/common")
---@type MAIN_TARGET_TYPE_CONFIG
local MAIN_TARGET_TYPE_CONFIG = import("bt/custom_node/enum_type_cfg/MAIN_TARGET_TYPE_CONFIG")
local main_target_weight_options = MAIN_TARGET_TYPE_CONFIG.main_target_weight_options
local M = {
    name = "is_exist_random_target",
    type = "Condition",
    desc = "检查目标是否存在随机目标列表中",
    args = {
        --{ name = "str", type = "string", desc = "变量描述" },
        {
            name = "nType",
            type = "enum",
            desc = "检查类型",
            options = {
                {
                    name = "配置的所有权重目标",
                    value = 1,
                },
                {
                    name = "当前角色的权重配置",
                    value = 2,
                },
            }
        },
        {
            name = "mainTargetType",
            type = "enum",
            desc = "目标类型",
            options = main_target_weight_options
        },
        {
            name = "dungeonName",
            type = "enum?",
            desc = "功能类型",
            options = {
                { name = '空', value = '' },
                { name = '繁荣之地', value = '繁荣之地' },
                { name = '伊莱塔神殿', value = '伊莱塔神殿' },
                { name = '马萨尔塔冰洞', value = '马萨尔塔冰洞' },
                { name = '圣科纳遗址', value = '圣科纳遗址' },
                { name = '冬季庆典', value = '冬季庆典' },
            }
        },
    },
    input = {},
    output = {},
}

function M.run(node, env, value)
    -- node.args.str
    local checkTarget = node.args.mainTargetType
    local nType = node.args.nType
    local owner = env.owner
    local randomTargetList = {}

    if nType == 1 then
        -- 配置目标
        local weightConfig = owner.main_target_weight_config
        for _, cfg in ipairs(weightConfig) do
            local config = cfg.config
            -- TODO:增加等级判断
            for k, v in ipairs(config) do
                table.insert(randomTargetList, v)
            end
        end
    elseif nType == 2 then
        -- 当前角色的权重配置
        randomTargetList = owner.curMainTargetWeightConfig
    end

    if not randomTargetList or table.is_empty(randomTargetList) then
        trace.log_trace('没有权重配置')
        return bret.FAIL
    end
    --print_r(randomTargetList)
    local is_exist = false
    for _, cfg in ipairs(randomTargetList) do
        if checkTarget == "副本" then
            local dungeonName = node.args.dungeonName
            local arr = cfg.arr
            --print_r(cfg)
            if arr and not table.is_empty(arr) then
                for k, targetInfo in ipairs(arr) do
                    local data = targetInfo.data
                    local mainTarget = targetInfo.mainTarget
                    local mainName = data.mainName
                    if dungeonName == mainName then
                        is_exist = true
                        break
                    end
                end
            end
        else
            local mainTarget = cfg.mainTarget
            if checkTarget == mainTarget then
                is_exist = true
                break
            end
        end
    end
    if is_exist then
        return bret.SUCCESS
    end
    return bret.FAIL
end

return M

-- table: 00000256CC54BFA0 {
--  [1] => table: 00000256CC54BFA0 {
--           [mainTarget] => "主线"
--           [isSet] => true
--           [randomWeight] => 100
--         }
--  [2] => table: 00000256CC54BFA0 {
--           [mainTarget] => "主线2"
--           [isSet] => false
--           [randomWeight] => 120
--         }
--  [3] => table: 00000256CC54BFA0 {
--           [arr] => table: 0000025646F38320 {
--                      [1] => table: 0000025646F38560 {
--                               [data] => table: 0000025646F38EE0 {
--                                           [mainName] => "繁荣之地"
--                                         }
--                               [mainTarget] => "进入副本"
--                             }
--                      [2] => table: 0000025646F38560 {
--                               [data] => table: 0000025646F38620 {
--                                           [mainName] => "繁荣之地"
--                                         }
--                               [mainTarget] => "副本挂机"
--                             }
--                    }
--           [randomWeight] => 100
--           [isSet] => true
--         }
--  [4] => table: 00000256CC54BFA0 {
--           [arr] => table: 0000025646F39360 {
--                      [1] => table: 0000025646F39CE0 {
--                               [data] => table: 0000025646F39D60 {
--                                           [mainName] => "马萨尔塔冰洞"
--                                         }
--                               [mainTarget] => "进入副本"
--                             }
--                      [2] => table: 0000025646F39CE0 {
--                               [data] => table: 0000025646F3A320 {
--                                           [mainName] => "马萨尔塔冰洞"
--                                           [executeTime] => 7200
--                                         }
--                               [mainTarget] => "副本挂机"
--                             }
--                    }
--           [randomWeight] => 110
--           [isSet] => false
--         }
--  [5] => table: 00000256CC54BFA0 {
--           [mainTarget] => "每日任务"
--           [isSet] => false
--           [randomWeight] => 100
--         }
--  [6] => table: 00000256CC54BFA0 {
--           [mainTarget] => "支线"
--           [isSet] => false
--           [randomWeight] => 100
--         }
--  [7] => table: 00000256CC54BFA0 {
--           [arr] => table: 0000025646F3A3E0 {
--                      [1] => table: 0000025646F3A660 {
--                               [data] => table: 0000025646F3B9A0 {
--                                           [mainName] => "圣科纳遗址"
--                                         }
--                               [mainTarget] => "进入副本"
--                             }
--                      [2] => table: 0000025646F3A660 {
--                               [data] => table: 0000025646F3B8E0 {
--                                           [mainName] => "圣科纳遗址"
--                                         }
--                               [mainTarget] => "副本挂机"
--                             }
--                    }
--           [randomWeight] => 100
--           [isSet] => false
--         }
--  [8] => table: 00000256CC54BFA0 {
--           [isSet] => false
--           [mainTarget] => "激战地挂机"
--           [randomWeight] => 75
--           [data] => table: 0000025646F3D460 {
--                       [mainName] => "激战地挂机"
--                       [executeTime] => 7200
--                     }
--         }
--}
----------------------------------------------------


-- table: 00000256D16893F0 {
--  [1] => table: 00000256D16893F0 {
--           [arr] => table: 00000256964DAAA0 {
--                      [1] => table: 00000256964DA2A0 {
--                               [data] => table: 00000256964DB120 {
--                                           [mainName] => "繁荣之地"
--                                         }
--                               [mainTarget] => "进入副本"
--                             }
--                      [2] => table: 00000256964DA2A0 {
--                               [data] => table: 00000256964DA460 {
--                                           [mainName] => "繁荣之地"
--                                         }
--                               [mainTarget] => "副本挂机"
--                             }
--                    }
--           [randomWeight] => 100
--           [isSet] => true
--         }
--  [2] => table: 00000256D16893F0 {
--           [arr] => table: 00000256964DA260 {
--                      [1] => table: 00000256964DA420 {
--                               [data] => table: 00000256964DA560 {
--                                           [mainName] => "马萨尔塔冰洞"
--                                         }
--                               [mainTarget] => "进入副本"
--                             }
--                      [2] => table: 00000256964DA420 {
--                               [data] => table: 00000256964DA4A0 {
--                                           [mainName] => "马萨尔塔冰洞"
--                                           [executeTime] => 7200
--                                         }
--                               [mainTarget] => "副本挂机"
--                             }
--                    }
--           [randomWeight] => 110
--           [isSet] => false
--         }
--  [3] => table: 00000256D16893F0 {
--           [mainTarget] => "每日任务"
--           [isSet] => false
--           [randomWeight] => 100
--         }
--  [4] => table: 00000256D16893F0 {
--           [mainTarget] => "支线"
--           [isSet] => false
--           [randomWeight] => 100
--         }
--  [5] => table: 00000256D16893F0 {
--           [isSet] => false
--           [mainTarget] => "激战地挂机"
--           [randomWeight] => 75
--           [data] => table: 00000256964DB7E0 {
--                       [mainName] => "激战地挂机"
--                       [executeTime] => 7200
--                     }
--         }
--}




